<?php
require_once '../Model/Level/OPPowerBar.php';
require_once '../Model/Player/OPRebelsFaction.php';
require_once '../Model/Player/OPOppressorsFaction.php';
require_once '../Model/Player/OPOppressor.php';
require_once '../Model/Player/OPRebel.php';
require_once '../Model/SafeHaven/OPSafeHaven.php';
require_once '../Model/SafeHaven/OPVirtualCharacter.php';
require_once '../Model/Geolocation/OPPosition.php';





class OPGame {
	/**
	* -------------------------------------------------------------------------
	*
	* 									Attributes
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Rebels faction
	 *
	 * @var OPRebelsFaction
	 */
	protected $rebelsFaction;
	
	/**
	* Oppressors faction
	*
	* @var OPOppressorsFaction
	*/
	protected $oppressorsFaction;
	
	/**
	* Safe Havens list
	*
	* @var ArrayList<OPSafeHaven>
	*/
	protected $safeHavens;
	
	/**
	 * -------------------------------------------------------------------------
	 *
	 * 									Static
	 *
	 * -------------------------------------------------------------------------
	 */
	
	/**
	 * Static constructor creating a new Game with all its parameters
	 * 
	 * @return OPGame
	 */
	public static function newGame(){
		$newGame = new OPGame();
		
		$newGame->initialize();
		
		return $newGame;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	*
	* 									Dynamic
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	* -------------------------------------------------------------------------
	* 									Game initialization
	* -------------------------------------------------------------------------
	*/
	
	/**
	* Initialize the game
	*
	* @return OPGame
	*/
	public function initialize() {
		/**
		 * Power bar creation
		 */
		$rebelsPowerBar 	= OPPowerBar::newWithGoalAndNumberOfLevels(100, 5);
		$oppressorsPowerBar = OPPowerBar::newWithGoalAndNumberOfLevels(100, 5);
		
		/**
		 * Factions creation
		 */
		$rebelsFaction		= OPRebelsFaction::newWithPowerBar($rebelsPowerBar);
		$oppressorsFaction	= OPOppressorsFaction::newWithPowerBar($oppressorsPowerBar);
		
		/**
		 * Oppressors are init with 20 points
		 */
		$oppressorsFaction->powerBar()->increaseBy(20);
		
		/**
		 * Set the factions to the game
		 */
		$this	->setRebelsFaction($rebelsFaction)
				->setOppressorsFaction($oppressorsFaction);
		
		/**
		 * Safe havens creation
		 */
		$safeHaven1 = OPSafeHaven::newWithIdPositionVirtualCharacterAndOwner(	1,
																				OPPosition::newWithLatitudeLongitude(45.912543, 6.125897), 
																				new OPVirtualCharacter(), 
																				OPFaction::OPPRESSORS_FACTION);
		$safeHaven2 = OPSafeHaven::newWithIdPositionVirtualCharacterAndOwner(	2,
																				OPPosition::newWithLatitudeLongitude(45.9052281, 6.133715),
																				new OPVirtualCharacter(),
																				OPFaction::REBELS_FACTION);
		
		$safeHaven3 = OPSafeHaven::newWithIdPositionVirtualCharacterAndOwner(	3,
																				OPPosition::newWithLatitudeLongitude(45.918964, 6.156828),
																				new OPVirtualCharacter(),
																				OPFaction::OPPRESSORS_FACTION);
		$safeHaven3->setPlayersIn(2);
		
		/**
		 * Add the safe havens to the game
		 */
		$this	->addSafeHaven($safeHaven1)
				->addSafeHaven($safeHaven2)
				->addSafeHaven($safeHaven3);
		
		/**
		 * Virtual Players creation
		 */
		$rebel1 	= OPRebel::newWithEmeiAndPosition('2', OPPosition::newWithLatitudeLongitude(45.902842, 6.133715));
		$rebel2 	= OPRebel::newWithEmeiAndPosition('3', OPPosition::newWithLatitudeLongitude(45.912759, 6.125927));
		$oppressor1 = OPOppressor::newWithEmeiAndPosition('4', OPPosition::newWithLatitudeLongitude(45.918848, 6.157094));
		$oppressor2 = OPOppressor::newWithEmeiAndPosition('5', OPPosition::newWithLatitudeLongitude(45.918734, 6.156769));
		$oppressor3 = OPOppressor::newWithEmeiAndPosition('6', OPPosition::newWithLatitudeLongitude(45.902842, 6.133715));
		
		/**
		 * Add the players to the game
		 */
		$this->rebelsFaction()	->addMember($rebel1)
								->addMember($rebel2);
		
		$this->oppressorsFaction()	->addMember($oppressor1)
									->addMember($oppressor2)
									->addMember($oppressor3);
		
	}
	
	/**
	 * -------------------------------------------------------------------------
	 * 									Setters
	 * -------------------------------------------------------------------------
	 */
	
	/**
	 * Set the rebels facion
	 *
	 * @param $rebelsFaction
	 * @return OPGame
	 */
	public function setRebelsFaction($rebelsFaction) {
		$this->rebelsFaction = $rebelsFaction;
		
		return $this;
	
	}
	
	/**
	* Set the oppressors faction
	*
	* @param $oppressorsFaction
	* @return OPGame
	*/
	public function setOppressorsFaction($oppressorsFaction) {
		$this->oppressorsFaction = $oppressorsFaction;
	
		return $this;
	
	}
	
	/**
	* Add a safe haven
	*
	* @param $safeHaven
	* @return OPGame
	*/
	public function addSafeHaven($safeHaven) {
		$this->safeHavens[] = $safeHaven;
	
		return $this;
	
	}
	
	/**
	 * -------------------------------------------------------------------------
	 * 									Getters
	 * -------------------------------------------------------------------------
	 */
	
	/**
	 * Return the Rebels faction
	 *
	 * @return OPRebelsFaction
	 */
	public function rebelsFaction() {
		return $this->rebelsFaction;
	
	}
	
	/**
	* Return the oppressors faction
	*
	* @return OPOppressorsFaction
	*/
	public function oppressorsFaction() {
		return $this->oppressorsFaction;
	
	}
	
	/**
	* Return the safe havens
	*
	* @return ArrayList<OPSafeHaven>
	*/
	public function safeHavens() {
		return $this->safeHavens;
	
	}
	
	/**
	 * Return the safe haven with the specified id
	 * 
	 * @param $id
	 * @return OPSafeHaven
	 */
	public function safeHaven($id) {
		$expectedSafeHaven = null;
		
		foreach ($this->safeHavens() as $safeHaven) {
			if ($id == $safeHaven->id()) {
				$expectedSafeHaven = $safeHaven;
				
			}
			
		}
		
		return $expectedSafeHaven;
		
	}
	
	
	
}